# -*- coding;utf-8 -*-
"""
File name : FlatQuestion.PY
Program IDE : PyCharm
Create file time: 2022/4/27 0:03
File Create By Author : 朱清闯
"""
from django.db import models

node_dict = {'subject': 'courses', 'courses': 'chapters', 'chapters': 'sections',
             'sections': 'knowledgepoints', 'knowledgepoints': 'questions',
             'questions': None}


def flat_question(current_node, current_list):
    # 当前key为questions
    if "questions" == current_node:
        if len(current_list['questions']) > 0:
            return current_list['questions']
        else:
            return []
    else:
        next_list = current_list[current_node]
        current_node = node_dict[current_node]
        temp_questions_list = []
        for index in range(len(next_list)):
            temp_questions_list += flat_question(current_node, next_list[index])
        return temp_questions_list


def flat_retriever_questions(current_node, current_list):
    # 当前key为questions
    if "questions" == current_node:
        if len(current_list['questions']) > 0:
            return current_list['questions']
        else:
            return []
    else:
        next_list = current_list[current_node]
        current_node = node_dict[current_node]
        temp_questions_list = []
        for index in range(len(next_list)):
            temp_questions_list += flat_retriever_questions(current_node, next_list[index])
        return temp_questions_list
